/**
 * angular工厂
 * @type {angular.Module}
 */
//初始化angular
var app = angular.module("app", []);

//绑定下拉框
app.factory('selectFactory', function ($http, $q) {
    var selectFactory = {};
    selectFactory.companytype = [{"id": '', "name": "-- 请选择 --"}, {"id": "1", "name": "客户来源"}, {
        "id": "2",
        "name": "项目类型"
    }, {"id": "3", "name": "项目状态"},];
    selectFactory.companystatus = [{"id": '正常', "name": "正常"}, {"id": '禁用', "name": "禁用"},];
    return selectFactory;

});


//获取数据
app.factory('dataFactory', function ($http, $q) {
    var httpFactory = {};

    function httpRequestEvent(type, url, data) {
        var res = $q.defer();
        $http({
            method: type,
            url: url,
            cache: false,
            params: data
        }).then(function successCallback(response) {
            res.resolve(response.data); //解决
        }, function errorCallback(error) {
            res.reject(error); //拒绝
        });

        return res.promise;
    }

    //获取基础数据
    httpFactory.getBasicsData = function (type) {
        var res = $q.defer();
        if (type == 10) {
            //状态为固定内容
            var result = [];
            result.data = [
                {"id": '', "name": "--请选择--"},
                {"id": "正常", "name": "正常"},
                {"id": "禁用", "name": "禁用"},
            ];
            res.resolve(result.data);
        } else {
            httpRequestEvent('get', '/project/bus/select_data', {type: type}).then(function (result) {
                result.data.unshift({"id": "", "name": "-- 请选择 --"});
                res.resolve(result.data);
            });
        }
        return res.promise;
    };
    //获取角色基础数据
    httpFactory.getRoleData = function () {
        var res = $q.defer();
        httpRequestEvent('get', '/admin/bus/get_role_data').then(function (result) {
            result.data.unshift({"id": 0, "name": "-- 请选择 --"});
            res.resolve(result.data);
        });
        return res.promise;
    };
    //获取部门基础数据
    httpFactory.getDepartmentData = function () {
        var res = $q.defer();
        httpRequestEvent('get', '/admin/bus/get_department_data').then(function (result) {
            result.data.unshift({"id": 0, "name": "-- 请选择 --"});
            res.resolve(result.data);
        });
        return res.promise;
    };
    //获取职位基础数据
    httpFactory.getPositionData = function () {
        var res = $q.defer();
        httpRequestEvent('get', '/admin/bus/get_position_data').then(function (result) {
            result.data.unshift({"id": 0, "name": "-- 请选择 --"});
            res.resolve(result.data);
        });
        return res.promise;
    };
    //获取职位基础数据
    httpFactory.getPositionData = function () {
        var res = $q.defer();
        httpRequestEvent('get', '/admin/bus/get_position_data').then(function (result) {
            result.data.unshift({"id": 0, "name": "-- 请选择 --"});
            res.resolve(result.data);
        });
        return res.promise;
    };
    //获取意向业务单号
    httpFactory.getIntentionData = function () {
        var res = $q.defer();
        httpRequestEvent('get', '/admin/bus/get_intention_data').then(function (result) {
            result.data.unshift({"id": 0, "name": "-- 请选择 --"});
            res.resolve(result.data);
        });
        return res.promise;
    };
    //获取币种
    httpFactory.getCurrencyData = function () {
        var res = $q.defer();
        //状态为固定内容
        var result = [];
        result.data = [
            {"id": '', "name": "--请选择--"},
            {"id": "美元", "name": "美元"},
            {"id": "欧元", "name": "欧元"},
            {"id": "澳元", "name": "澳元"},
            {"id": "英镑", "name": "英镑"},
            {"id": "泰铢", "name": "泰铢"},
            {"id": "韩元", "name": "韩元"},
            {"id": "卢布", "name": "卢布"},
            {"id": "越南盾", "name": "越南盾"},
            {"id": "加元", "name": "加元"},
            {"id": "人民币", "name": "人民币"},
        ];
        res.resolve(result.data);
        return res.promise;
    };
    //获取基础数据
    httpFactory.getPaymentMethodData = function () {
        var res = $q.defer();
        //状态为固定内容
        var result = [];
        result.data = [
            {"id": '', "name": "--请选择--"},
            {"id": "现金_现金", "name": "现金_现金"},
            {"id": "现金_信用证", "name": "现金_信用证"},
            {"id": "现金_DP", "name": "现金_DP"},
            {"id": "信用证_现金", "name": "信用证_现金"},
            {"id": "信用证_DP", "name": "信用证_DP"},
        ];
        res.resolve(result.data);
        return res.promise;
    };
    //获取省份
    httpFactory.getProvinceData = function () {
        var res = $q.defer();
        //状态为固定内容
        var result = [];
        result.data = [];
        for (var i = 1; i < area_json.length; i++) {
            result.data.push({"value": i, "name": area_json[i].name});
        }
        res.resolve(result.data);
        return res.promise;
    };
    //获取城市
    httpFactory.getCity = function (citys) {
        var res = $q.defer();
        //状态为固定内容
        var result = [];
        result.data = [];
        for (var i = 1; i < citys.sub.length; i++) {
            result.data.push({"value": i, "name": citys.sub[i].name});
        }
        res.resolve(result.data);
        return res.promise;
    };
    //获取订单类型
    httpFactory.getType = function () {
        var res = $q.defer();
        //状态为固定内容
        var result = [];
        result.data = [
            {"id": 0, "name": "--请选择--"},
            {"id": 1, "name": "现货"},
            {"id": 2, "name": "期货"},
        ];
        res.resolve(result.data);
        return res.promise;
    };

    //客户来源
    httpFactory.SelectCustomer = function () {
        return httpFactory.getBasicsData(1);
    };
    //项目类型
    httpFactory.SelectTemplate = function () {
        return httpFactory.getBasicsData(2);
    };
    //项目状态
    httpFactory.SelectTemplateItemType = function () {
        return httpFactory.getBasicsData(3);
    };


    //状态
    httpFactory.SelectStatus = function () {
        return httpFactory.getBasicsData(10);
    };

    //角色
    httpFactory.SelectRole = function () {
        return httpFactory.getRoleData();
    };
    //部门
    httpFactory.SelectDepartment = function () {
        return httpFactory.getDepartmentData();
    };
    //职位
    httpFactory.SelectPosition = function () {
        return httpFactory.getPositionData();
    };
    //币种
    httpFactory.SelectCurency = function () {
        return httpFactory.getCurrencyData();
    };
    //付款方式
    httpFactory.SelectPaymentMethod = function () {
        return httpFactory.getPaymentMethodData();
    };
    //省份
    httpFactory.SelectProvince = function () {
        return httpFactory.getProvinceData();
    };
    //城市
    httpFactory.SelectCity = function (citys) {
        return httpFactory.getCity(citys);
    };
    //订单类型
    httpFactory.SelectType = function () {
        return httpFactory.getType();
    };
    return httpFactory;
});
